package com.demo2.todo.controller;

import com.demo2.todo.model.dto.ReportProcessRequest;
import com.demo2.todo.model.dto.ReportRequest;
import com.demo2.todo.model.dto.ReportResponse;
import com.demo2.todo.service.ReportService;
import com.demo2.todo.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author evi
 */
@RestController
@RequestMapping("/reports")
@RequiredArgsConstructor
public class ReportController {
    private final ReportService reportService;

    @PostMapping("/{todoId}")  // 修改路径以匹配前端
    public ResponseEntity<ReportResponse> createReport(
            @PathVariable Long todoId,  // 从路径获取todoId
            @RequestBody ReportRequest request) {
        // 将路径中的todoId设置到请求对象中
        request.setTodoId(todoId);
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(reportService.createReport(request, SecurityUtils.getCurrentUser()));
    }

    // 管理员获取所有举报
    @GetMapping("/admin")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<List<ReportResponse>> getAllReports() {
        return ResponseEntity.ok(reportService.getAllReports());
    }

    // 管理员处理举报
    @PutMapping("/{id}/process")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ReportResponse> processReport(
            @PathVariable Long id,
            @RequestBody ReportProcessRequest request) {
        return ResponseEntity.ok(reportService.processReport(id, request, SecurityUtils.getCurrentUser()));
    }
}